package cfca.seal.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateDateModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNumberModel;
import freemarker.template.TemplateScalarModel;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

public class FreemarkerUtil
{
  public static boolean castDirectiveParamToBoolean(TemplateModel templateModel)
    throws TemplateModelException
  {
    boolean bool = false;
    if ((templateModel instanceof TemplateBooleanModel)) {
      bool = ((TemplateBooleanModel)templateModel).getAsBoolean();
    }
    return bool;
  }

  public static byte castDirectiveParamToByte(TemplateModel templateModel) throws TemplateModelException {
    return castDirectiveParamToNumber(templateModel).byteValue();
  }

  public static Date castDirectiveParamToDate(TemplateModel templateModel) throws TemplateModelException {
    Date date = null;
    if ((templateModel instanceof TemplateDateModel)) {
      date = ((TemplateDateModel)templateModel).getAsDate();
    }
    return date;
  }

  public static double castDirectiveParamToDouble(TemplateModel templateModel) throws TemplateModelException {
    return castDirectiveParamToNumber(templateModel).doubleValue();
  }

  public static float castDirectiveParamToFloat(TemplateModel templateModel) throws TemplateModelException {
    return castDirectiveParamToNumber(templateModel).floatValue();
  }

  public static int castDirectiveParamToInt(TemplateModel templateModel) throws TemplateModelException {
    return castDirectiveParamToNumber(templateModel).intValue();
  }

  public static long castDirectiveParamToLong(TemplateModel templateModel) throws TemplateModelException {
    return castDirectiveParamToNumber(templateModel).longValue();
  }

  public static short castDirectiveParamToShort(TemplateModel templateModel) throws TemplateModelException {
    return castDirectiveParamToNumber(templateModel).shortValue();
  }

  public static Number castDirectiveParamToNumber(TemplateModel templateModel) throws TemplateModelException {
    Number number = null;
    if ((templateModel instanceof TemplateNumberModel)) {
      number = ((TemplateNumberModel)templateModel).getAsNumber();
    }
    return number;
  }

  public static String castDirectiveParamToString(TemplateModel templateModel) throws TemplateModelException {
    String str = null;
    if ((templateModel instanceof TemplateScalarModel)) {
      str = ((TemplateScalarModel)templateModel).getAsString();
    }
    return str;
  }

  public static String getText(FreeMarkerConfigurer freeMarkerConfigurer, String templateFileName, Map map) throws IOException, TemplateException {
    String htmlText = "";
    Template tpl = freeMarkerConfigurer.getConfiguration().getTemplate(templateFileName);
    htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, map);
    return htmlText;
  }
}